﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login_register_inc.aspx.cs" Inherits="newBlog.CenterInc.view_inc.login_inc" %>

<%@ Import Namespace="newBlog.CenterInc.Ctrltor" %>
<%@ Import Namespace="newBlog.CenterInc.model" %>
<%@ Import Namespace="newBlog.CenterInc.tool" %>
<%   
    Response.ContentType = "application/json";

    int mode = Convert.ToInt32(Request.QueryString["mode"]);//登录1，注册2，3注销,用来判断用途 4重置密码
    String username = Request.Form["username"];
    String password = Request.Form["password"];
    string EmailVer = Request.Form["EmailVer"];//前台接收的验证码
    if (mode==3) {
        Session.Clear();
        Response.Write("{\"msg\":\"注销成功\"}");
        return;
    }
    if (Session["user_id"] != null)
    {
        Response.Write("{\"msg\":\"您已登录\"}");
        return;
    }
    User_inc user_Inc = new User_inc();
    isString isString = new isString();
    if (mode==4) {
        string emailName=Request.Form["emailName"];
        string ReVer = Session[emailName + "RePwdlVer"]==null?"":Session[emailName + "RePwdlVer"].ToString();
        if (isString.isEmpty(username,emailName,EmailVer,ReVer)) {
            Response.Write("{\"error\":\"您未获取验证码或者您用户名邮箱以及验证码不能为空...\"}");
            return;
        }
        if (!ReVer.Equals(EmailVer)) {
            Response.Write("{\"error\":\"验证码输入有误...\"}");
            return;
        }
        if (user_Inc.User_nameAndEmail(username,emailName)) { 
            Response.Write("{\"error\":\"用户名或者邮箱有误，请重新输入\"}");
                return;
            }
        if (user_Inc.ResetPassword(username,emailName)) {
            Response.Write("{\"error\":\"重置密码失败...\"}");
            return;
        }
        Session.Remove(emailName + "RePwdlVer");
        Response.Write("{\"msg\":\"重置密码成功默认密码为：A123456\"}");
        return;
    }//重置密码
    if (isString.isEmpty(username, password, EmailVer))
    {
        Response.Write("{\"error\":\"用户名密码以及验证码不能为空...\"}");
        return;
    }
    if (isString.IsRange(6, 20, username, password))
    {
        Response.Write("{\"error\":\"用户名密码需要在5~20个字符...\"}");
        return;
    }
    if (isString.IsChar(@"^[^0-9a-z._]+$", username, password))
    {
        Response.Write("{\"error\":\"用户名密码不能包含特殊字符哦...\"}");
        return;
    }
    switch (mode)
    {
        case 1:

            if (!EmailVer.Equals(Session["login_ver"].ToString()))
            {
                Response.Write("{\"error\":\"验证码错误\"}");
                return;
            }
            Session.Remove("login_ver");
            if (user_Inc.SelectedUser(username, password) < 1)
            {
                Response.Write("{\"error\":\"登陆失败，用户名或密码错误\"}");
                return;
            }
            LoginDao loginDao = user_Inc.UserLoginMsg(username, password);
            Session.Add("user_id", loginDao.Id);
            Session.Add("user_name", loginDao.Username);
            Session.Add("user_pwd", loginDao.Password);
            Session.Add("user_grade", loginDao.Grade);
            Session.Add("user_email", loginDao.Email);
            Response.Write("{\"msg\":\"登陆成功欢迎您，" + loginDao.Username + "\"}");

            return;
        case 2:
            string emailname = Request.Form["emailname"];
            if (isString.isEmpty(emailname))
            {
                Response.Write("{\"error\":\"邮箱不能为空\"}");
                return;
            }
            if (!isString.IsChar(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", emailname))
            {
                Response.Write("{\"error\":\"邮箱格式不正确哦！\"}");
                return;
            }
            if (!Session[emailname + "register_ver"].Equals(EmailVer))
            {
                Response.Write("{\"error\":\"验证码错误\"}");
                return;

            }
            if (user_Inc.isExist(username)) {
                Response.Write("{\"error\":\"用户名存在\"}");
                return;
            }
            if (user_Inc.UserRegister(new registerDao(username, password, emailname)))
            {
                Response.Write("{\"error\":\"注册失败\"}");
                return;
            }
            Response.Write("{\"msg\":\"注册成功\"}");
            return;
        default:
            Response.Write("{\"msg\":\"无任何内容\"}");
            return;
    }



%>
